package com.onpositive.notes.android.ui.elements;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;

import com.onpositive.businessdroids.model.IField;
import com.onpositive.businessdroids.model.impl.AbstractComputableField;
import com.onpositive.businessdroids.ui.IOpenListener;
import com.onpositive.businessdroids.ui.dataview.DataViewFactory;
import com.onpositive.businessdroids.ui.dataview.StructuredDataView;
import com.onpositive.collections.IObjectCollection;
import com.onpositive.notes.android.main.CollectionHistoryActivity;
import com.onpositive.notes.android.main.CommonResources;
import com.onpositive.notes.android.main.EditNoteActivity;
import com.onpositive.notes.model.INote;
import com.onpositive.notes.services.BasicCollectionRegistry;

public class CollectionsListUI {
	
	public static final String EXTRA_KEY_COLLECTION = "key-collection";
	
	private StructuredDataView sdv;

	public View createView( final Context context ){
		
		initDataView( context );		
		LinearLayout lt = new LinearLayout(context);
		lt.addView(sdv);
		
		
		
		return lt ;
	}
	
	public void initDataView( final Context context )
	{
		IField contentField = new AbstractComputableField() {

			@Override
			public Object getPropertyValue(Object object) {

				if (object == null)
					return "NULL";

				if (object instanceof INote)
					return ((INote) object).getText(CommonResources.defaultLocale);

				return object.toString();
			}
		};

		IField[] fields = new IField[]{ contentField  } ;//, dateField } ;
		
		sdv = DataViewFactory.createTable(
				context, BasicCollectionRegistry.getInstance().getAllCollections(), null, null, fields);
		
		sdv.setActionBarVisible(true) ;
		//sdv.setHeaderVisible(true) ;
		//sdv.setFooterVisible(true) ;

		sdv.setOpenListener(new IOpenListener() {

			@Override
			public void openElement(View parent, View elementView, Object element, int pos)
			{
				IObjectCollection collection = (IObjectCollection) element;

				Intent intent = new Intent(context, CollectionHistoryActivity.class);
				intent.putExtra(EXTRA_KEY_COLLECTION, collection);
				context.startActivity(intent) ;
			}
		});		
	}
}
